package com.global.college.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.global.college.MainActivity;
import com.global.college.R;
import com.global.college.api.BaseResponseModel;
import com.global.college.base.BaseActivity;
import com.global.college.ui.interfaces.IRegistView;
import com.global.college.ui.presenter.RegistPresenter;
import com.global.college.utils.NetworkUtils;
import com.global.college.utils.RxTimer;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.ClearEditText;
import com.global.college.views.TitleBar;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册
 */
public class RegistActivity extends BaseActivity implements TitleBar.TitleBarCallback, IRegistView {

    @BindView(R.id.et_phone)
    ClearEditText etPhone;
    @BindView(R.id.et_sms_code)
    EditText etSmsCode;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_psw)
    ClearEditText etPsw;
    @BindView(R.id.iv_cipherText)
    ImageView ivCipherText;
    @BindView(R.id.ll_psw_show_hint)
    LinearLayout llPswShowHint;
    @BindView(R.id.tv_return_login)
    TextView tvReturnLogin;
    @BindView(R.id.cb_regist_lagel)
    CheckBox cbRegistLagel;
    @BindView(R.id.tv_regist_lagel)
    TextView tvRegistLagel;
    @BindView(R.id.tv_privacyPolicy)
    TextView tvPrivacyPolicy;
    @BindView(R.id.iv_regist)
    ImageView ivRegist;
    private RxTimer rxTimer;
    private boolean isVisible = true;
    private RegistPresenter registPresenter;
    private String strPhone, strPassword, strCode;
    private int i = 60;
    private boolean isSend = false;
    @Override
    public int getContentViewId() {
        return R.layout.activity_regist;
    }



    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(false);

    }

    @Override
    public void getInitData() {
        if (rxTimer == null) {
            rxTimer = new RxTimer();
        }
        registPresenter = new RegistPresenter(this);
        registPresenter.setRegistView(this);
        initLiseners();
    }

    private void initLiseners() {
        cbRegistLagel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                if (checked) {
                    ivRegist.setEnabled(true);
                    ivRegist.setImageResource(R.mipmap.iocn_login_enable);
                } else {
                    ivRegist.setEnabled(false);
                    ivRegist.setBackgroundResource(R.mipmap.iocn_login_disable);
                    ToastUtils.showCenterToast(R.string.success_this_terms_to_use_soft);
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        useImmersionBar =false;
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.tv_send_code, R.id.ll_psw_show_hint, R.id.iv_regist, R.id.tv_return_login, R.id.tv_regist_lagel, R.id.tv_privacyPolicy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send_code:
                getCode();
                break;
            case R.id.ll_psw_show_hint:
                if (isVisible) {
                    //密码输入框的id login_pwd
                    etPsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    isVisible = false;
                    ivCipherText.setBackgroundResource(R.mipmap.icon_show_psw);
                    etPsw.setSelection(etPsw.length());
                } else {
                    etPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    isVisible = true;
                    ivCipherText.setBackgroundResource(R.mipmap.icon_hint_psw);
                    etPsw.setSelection(etPsw.length());
                }
                break;
            case R.id.iv_regist:
                regist();
                break;
            case R.id.tv_return_login:
                openActivity(LoginByPswActivity.class);
                break;
            case R.id.tv_regist_lagel:
                startActivity(new Intent(this, HsLagelDetailActivity.class).putExtra("type", "0"));
                break;
            case R.id.tv_privacyPolicy:
                startActivity(new Intent(this, HsLagelDetailActivity.class).putExtra("type", "1"));
                break;
        }
    }



    @Override
    public void onBackPress() {
        finish();
    }

    @Override
    public void onRightPress() {

    }

    @Override
    public void onRightImageClick() {

    }

    @Override
    public void onSearchImageClick() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        rxTimer.cancel();
        rxTimer = null;

    }


    @Override
    public void onRegistSuccess(BaseResponseModel registModel) {
        ToastUtils.showToastShort(R.string.regist_success);
        openActivity(LoginByPswActivity.class);
    }

    @Override
    public void onRegistFail(String error) {

    }

    @Override
    public void onGetSmsCodeSuccess(Object smsModel) {
        ToastUtils.showToastShort(R.string.send_success);
    }

    @Override
    public void onGetSmsCodeFail(String error) {

    }
    /**
     * 获取验证码
     */
    private void getCode() {
        strPhone = etPhone.getText().toString();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showToastShort(R.string.your_device_has_not_netword);
            return;
        } else {
            if (StringUtils.isEmpty(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (!StringUtils.isPhone(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (isSend){
                ToastUtils.showToastShort(R.string.please_wait_moment);
                return;
            }
            //发送
            registPresenter.getSmsCode(strPhone);
            startTimer();
        }
    }

    /**
     * 注册
     */
    private void regist() {
        strPhone = etPhone.getText().toString();
        strPassword = etPsw.getText().toString();
        strCode = etSmsCode.getText().toString();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showToastShort(R.string.your_device_has_not_netword);
            return;
        } else {
            if (StringUtils
                    .isEmpty(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            } else if (6 > StringUtils.getLength(strPassword, true)
                    || 16 < StringUtils.getLength(strPassword, true)) {
                ToastUtils.showToastShort(
                        R.string.please_input_from_six_to_sixteen_psw);
                return;
            } else if (StringUtils.isEmpty(strCode)) {
                ToastUtils.showToastShort(
                        R.string.sms_code_not_empty);
                return;
            } else if (!StringUtils.isNumber(strCode)) {
                ToastUtils.showToastShort(R.string.sms_code_format_error);
                return;
            } else {
                if (!cbRegistLagel.isChecked()){
                    ToastUtils.showCenterToast(R.string.success_this_terms_to_use_soft);
                    return;
                }
                //注册
                registPresenter.getRegist(strPhone, strCode,strPassword,"");
            }
        }
    }
    /**
     * 开始计时任务
     */
    private void startTimer() {
        if (rxTimer == null) {
            rxTimer = new RxTimer();
        }
        rxTimer.interval(1000, new RxTimer.RxAction() {
            @Override
            public void action(long number) {
                Log.e("打印时间：","----"+number);
                if (number == i) {
                    tvSendCode.setText(R.string.resend);
                    isSend = false;
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_fff73043));
                    rxTimer.cancel();

                } else {
                    tvSendCode.setText("(" + (i-number) + ")"+getString(R.string.sended));
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_999999));
                    isSend = true;
                }
            }
        });
    }
}